package com.bardpub.weblite.system 
{
	import adobe.utils.ProductManager;
	import com.bardpub.weblite.connect.WebRequest;
	import com.bardpub.weblite.ui.PageInfo;
	import com.bardpub.weblite.utils.Broswer;
	import com.bardpub.weblite.WebLiteMain;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.system.System;
	
	/**
	 * Copyright © 2010-2012, BardPub.com
	 * @fileName WebConfig.as
	 * @author LittleBard
	 * @version 1.0
	 * @date 2010-5-5
	 * @descrition 网站配置类
	 * @others
	 * @history
	 */
	
	 
	public class WebConfig extends EventDispatcher
	{
		public static const debug:Boolean = true;
		public static const INVALIDATE:String = "invalidate";
		private static var __instance:WebConfig;
		
		private var __config:XML;
		private var __weblite:WebLiteMain;
		
		/**
		 * 创建com.bardpub.weblite.system.WebConfig 类的新实例
		 * 禁止直接实例化，请使用WebConfig.getInstance()来获取全局唯一实例
		 */
		public function WebConfig() 
		{
			
		}
		
		/**
		 * 获取网站config
		 */
		public function get config():XML
		{
			return __config;
		}
		
		public function getData(name:String,...args):String
		{
			
			if (__config == null) return null;
			var result:String;
			if (__config.data[name].children().length() > 1)
			{
				result = __config.data[name].url;
				var fields:XMLList = __config.data[name].field;
				for (var i:int = 0, len:int = fields.length(); i < len; i++)
				{
					result = result.replace(fields[i], args[i]);
				}
			}
			else 
			{
				result = __config.data[name];
			}
			return result;
		}
		
		/**
		 * 注册网站主框架
		 * @param	weblite
		 */
		public function register(weblite:WebLiteMain):void 
		{
			__weblite = weblite;
		}
		
		/**
		 * 转到子页面
		 * @param	pageDir
		 */
		public function goPage(pageDir:String):void 
		{
			System.gc();
			System.gc();
			System.gc();
			if (__weblite == null)
			{
				return;
			}
			var info:PageInfo = getPageInfo(pageDir);
			
			
			if (info && info.pageType == "external")
			{
			
				Broswer.open(info.pageURL, info.pageTarget);
				return;
			}
			
			setLocation(pageDir);
		
			
			__weblite.loadPage(info);
		}
		
		/**
		 * 设置浏览器地址栏参数
		 * @param	dir
		 */
		protected function setLocation(dir:String):void 
		{
			if (!debug)
			{
				if (dir != null)
				{
					Broswer.setHash(dir);
				}
			}
		}
		
		
		public function getChildrenName():String 
		{
			if (__config.website.mainchild.length() > 0)
			{
				return __config.website.mainchild[0].toString();
			}
			return null;
		}
		
		
		/**
		 * 获取WebConfig类的唯一实例
		 * @return WebConfig类的唯一实例
		 */
		public static function getInstance():WebConfig
		{
			if (__instance == null)
			{
				__instance = new WebConfig();
			}
			return __instance;
		}

		/**********************************************************/
		/*===                                                  ===*/
		/*===                 public methods                   ===*/
		/*===                                                  ===*/
		/**********************************************************/
		
		/**
		 * 初始化
		 */
		public function initialize(cfgURL:String):void 
		{
			WebRequest.getInstance().load(cfgURL, 
			                              WebRequest.TYPE_XML,
										  onLoadConfigComplete,
										  null,
										  onLoadConfigFailed);
		}
		
		/**
		 * 根据页面标签获取页面URL
		 * @param	pageDir 页面标签
		 * @return 	PageINfo 类的实例
		 */
		public function getPageInfo(pageDir:String):PageInfo 
		{
			var pageInfo:PageInfo;
			if (pageDir == null)
			{
				pageInfo = getDefaultPageInfo();
			}
			else 
			{
				var tmpArr:Array = pageDir.split("/");
				var tmpInfo:Array = new Array();
				var tmplist:XMLList = __config.page.doc;
				for (var i:int = 0, len:int = tmpArr.length; i < len; i++ )
				{
					var xml:XML = getDocXMLByName(tmpArr[i], tmplist);
					if (xml == null)
					{
						break;
					} 
					else 
					{	
						var info:PageInfo = new PageInfo();
						info.pageName = xml.@name;
						info.pageTitle = xml.@title;
						info.pageType = xml.@type;
						info.pageURL = xml.@url;
						info.pageTarget = xml.@target;
						if (tmpInfo.length > 0) 
						{
							info.parent = tmpInfo[tmpInfo.length - 1];
						}
						tmpInfo.push(info);
						tmplist = xml.doc;
						if (i == len - 1)
						{
							while (xml != null && xml.@autochild != "")
							{
								var autochild:String = xml.@autochild;
								var getc:Boolean = false;
								for (var k:int = 0; !getc &&  k < xml.doc.length() ; k++ )
								{
									var kxml:XML = xml.doc[k];
									if (kxml.@name == autochild)
									{
										var kinfo:PageInfo = new PageInfo();
										kinfo.pageName = kxml.@name;
										kinfo.pageTitle = kxml.@title;
										kinfo.pageType = kxml.@type;
										kinfo.pageURL = kxml.@url;
										kinfo.pageTarget = kxml.@target;
										kinfo.parent = tmpInfo[tmpInfo.length - 1];
										tmpInfo.push(kinfo);
										getc = true;
										xml = kxml;
										
									}
								}
								if (!getc)
								{
									xml = null;
								}
								
							}
						}
					}
				}
				pageInfo = tmpInfo[tmpInfo.length - 1] || getDefaultPageInfo();
			}
			
			return pageInfo;
		}
		
		/**
		 * 获取默认首页 PageInfo
		 * @return PageInfo
		 */
		private function getDefaultPageInfo():PageInfo 
		{
			var pageInfo:PageInfo = new PageInfo();
			var tmpDoc:XML = __config.page.doc[0];
			pageInfo.pageName = tmpDoc.@name;
			pageInfo.pageTitle = tmpDoc.@title;
			pageInfo.pageType = tmpDoc.@type;
			pageInfo.pageURL = tmpDoc.@url;
			return pageInfo;
		}
		
		/**
		 * 从XMLList中赛选pageName符合的项
		 * @param	name	pageName
		 * @param	list	page XMLList
		 * @return 	XML(document)
		 */
		private function getDocXMLByName(name:String, list:XMLList):XML
		{
			for (var i:int = 0, len:int = list.length(); i < len; i++ )
			{
				if (list[i].@name == name) 
				{
					return list[i];
				}
			}
			return null;
		}
		
		/**
		 * 获取当前页面标签
		 * @return 当前页面名
		 */
		public static function getCurrentLocation():String
		{
			if (debug)
			{
				return null;
			}
			else
			{
				return Broswer.GetHash();
			}
		}
		
		/**********************************************************/
		/*===                                                  ===*/
		/*===           protected/private methods              ===*/
		/*===                                                  ===*/
		/**********************************************************/
		
		/**
		 * 当配置文件载入完成后触发
		 * @param	data
		 */
		private function onLoadConfigComplete(data:*):void
		{
			__config = XML(data);
			dispatchEvent(new Event(INVALIDATE));
		}
		
		
		/**           
		 * 当配置文件载入失败时触发
		 */
		private function onLoadConfigFailed():void 
		{
			Broswer.alert("Load config failed. please check the website config xml");
			throw new Error("Load config failed. please check the website config xml");
		}
	}

}